package com.bupt.liutong.admin.sch.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.sch.bo.NewsBo;
import com.bupt.liutong.admin.sch.form.NewsForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.DateUtils;

public class NewsAction extends BaseAction {

	private NewsBo newsBo;

	public ActionForward getNewsesByPage(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		NewsForm newsForm = (NewsForm) form;
		request.setAttribute("dispMap", ctm.get("ifDisp"));
		request.setAttribute("newsFormList", newsBo.getNewsesByPage(newsForm,
				request));
		putPageInfo(request, newsForm);
		return mapping.findForward("getNewsesByPageSuccess");
	}

	public ActionForward insertNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsForm newsForm = (NewsForm) form;
		newsForm.setCreateTime(DateUtils.getCurDate());
		newsForm.setTop(0);
		request.setAttribute("topList", getOptionList(ctm.get("yesOrNo"), false));
		return mapping.findForward("insertNewsSuccess");
	}

	public ActionForward insertNewsDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsForm newsForm = (NewsForm) form;
		newsBo.insertNews(newsForm);
		return mapping.findForward("insertNewsDoSuccess");
	}

	public ActionForward updateNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsForm newsForm = (NewsForm) form;
		BeanUtils.copyProperties(newsForm, newsBo.getNewsById(newsForm));
		request.setAttribute("topList", getOptionList(ctm.get("yesOrNo"), false));
		return mapping.findForward("updateNewsSuccess");
	}

	public ActionForward updateNewsDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsForm newsForm = (NewsForm) form;
		newsBo.updateNews(newsForm);
		return mapping.findForward("updateNewsDoSuccess");
	}

	public ActionForward enableNewses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		newsBo.enableNews(ids);
		return mapping.findForward("enableNewsesSuccess");
	}

	public ActionForward disableNewses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		newsBo.disableNews(ids);
		return mapping.findForward("disableNewsesSuccess");
	}

	public ActionForward deleteNewses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		newsBo.deleteNewses(ids);
		return mapping.findForward("deleteNewsesSuccess");
	}

	public void setNewsBo(NewsBo newsBo) {
		this.newsBo = newsBo;
	}
}
